<?php
namespace App\Service\Admin;

use Illuminate\Support\Facades\DB;

class FindService extends BaseService
{
    //二期
    //发现话题

    protected $types = [
        1 => '活动话题', '精选分享',
    ];
    protected $statuss = [
        '未审核', '已审核',
    ];

    /**
     * 列表
     */
    public function getList()
    {
        $db = $this->dbConn($this->dbFind)->where('del', 0);
        $total = $db->count();
        $res = $db->orderBy('is_top', 'desc')
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有话题');
        }
        $types = $this->types;
        $statuss = $this->statuss;
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            $type_name = array_key_exists($v['type'], $types) ? $types[$v['type']] : '-';
            $status_name = array_key_exists($v['status'], $statuss) ? $statuss[$v['status']] : '-';
            $uOne = (array) $this->dbConn($this->dbUser)->where('id', $v['uid'])->first();
            $uname = empty($uOne) ? '-' : $uOne['nickname'];
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'type_name' => $type_name,
                'uname' => $uname,
                'status' => $v['status'],
                'status_name' => $status_name,
                'top' => $v['is_top'],
                'addtime' => $v['addtime'],
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 审核
     * @param int $id
     */
    public function setAuth($id)
    {
        $res = (array) $this->dbConn($this->dbFind)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $up = $this->dbConn($this->dbFind)->where('id', $id)
            ->update(['status' => 1, 'uptime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, '审核失败');
        }
        return $this->toback();
    }

    /**
     * 删除
     * @param int $id
     */
    public function setDel($id)
    {
        $res = (array) $this->dbConn($this->dbFind)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $this->dbConn($this->dbFind)->where('id', $id)->update(['del' => 1]);
        return $this->toback();
    }

    /**
     * 详情
     * @param int $id
     */
    public function getOne($id)
    {
        $res = (array) $this->dbConn($this->dbFind)->where('id', $id)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $types = $this->types;
        $statuss = $this->statuss;
        $res['type_name'] = array_key_exists($res['type'], $types) ? $types[$res['type']] : '-';
        $res['status_name'] = array_key_exists($res['status'], $statuss) ? $statuss[$res['status']] : '-';
        //图片获取
        $imgList = [];
        $imgOnes = $this->dbConn($this->dbFindImg)->where('post_id', $id)->get();
        if (count($imgOnes)) {
            foreach ($imgOnes as $v) {
                $v = (array) $v;
                if (substr($v['link'], 0, 4) == 'http') {
                    $link = $v['link'];
                } else if (file_exists($v['link'])) {
                    $link = '/'.$v['link'];
                } else {
                    $link = '';
                }
                $imgList[] = $link;
            }
        }
        $res['imgList'] = $imgList;
        //发布人信息
        $uOne = (array) $this->dbConn($this->dbUser)->where('id', $res['uid'])->first();
        $res['puber'] = empty($uOne) ? '-' : $uOne['nickname'];
        return $this->toback(0, '', $res);
    }

    /**
     * 分类列表
     */
    public function cateList()
    {
        $db = $this->dbConn($this->dbFindCate)->where('del', 0)->where('parent_id', '>', 0);
        $total = $db->count();
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有话题');
        }
        $arys = [];
        foreach ($res as $k => $v) {
            $v = (array) $v;
            $pname = (array) $this->dbConn($this->dbFindCate)
                ->where('id', $v['parent_id'])
                ->value('cate_name');
            //$pname = $v['parent_id']==1 ? '活动话题' : '精选分享';
            $arys[] = [
                'id' => $v['id'],
                'cate_name' => $v['cate_name'],
                'pid' => $v['parent_id'],
                'pname' => $pname ? $pname : '-',
                'uptime' => $v['uptime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 分类修改
     */
    public function cateModify($id, $pas)
    {
        if ($id) {
            $res = (array) $this->dbConn($this->dbFindCate)
                ->where('id', $id)
                ->first();
            if (empty($res)) {
                return $this->toback(-2, '没有记录');
            }
            $pas['uptime'] = toDate();
            $up = $this->dbConn($this->dbFindCate)->where('id', $id)->update($pas);
            if (!$up) {
                return $this->toback(-1, 'error');
            }
        } else {
            $res = (array) $this->dbConn($this->dbFindCate)
                ->where('cate_name', $pas['cate_name'])
                ->first();
            if (!empty($res)) {
                return $this->toback(-2, '有同名记录');
            }
            $pas['addtime'] = $pas['uptime'] = toDate();
            $lastid = $this->dbConn($this->dbFindCate)->insertGetId($pas);
            if (!$lastid) {
                return $this->toback(-1, 'error');
            }
        }
        return $this->toback();
    }

    /**
     * 分类删除
     */
    public function cateDel($id)
    {
        $res = (array) $this->dbConn($this->dbFindCate)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $up = $this->dbConn($this->dbFindCate)->where('id', $id)->update(['del' => 1]);
        if (!$up) {
            return $this->toback(-1, 'error');
        }
        return $this->toback();
    }

    /**
     * 是否置顶
     * @param int $id
     * @param int $top
     */
    public function setTop($id, $top)
    {
        $res = (array) $this->dbConn($this->dbFind)
            ->where('id', $id)
            ->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $this->dbConn($this->dbFind)->where('id', $id)->update(['is_top' => $top]);
        return $this->toback();
    }

    /**
     * 获取开关
     */
    public function switchOne()
    {
        $res = (array) $this->dbwbConn($this->dbQdConfig)->where('id', 11)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        return $this->toback(0, '', $res['value']);
    }

    /**
     * 设置开关
     */
    public function switchModify($kg)
    {
        $res = (array) $this->dbwbConn($this->dbQdConfig)->where('id', 11)->first();
        if (empty($res)) {
            return $this->toback(-2, '没有记录');
        }
        $this->dbwbConn($this->dbQdConfig)->where('id', 11)->update(['value' => $kg]);
        return $this->toback();
    }
}